﻿using System.Reflection;
using KnownCMS.Pages.Users;

namespace KnownCMS;

/// <summary>
/// KnownCMS前台应用配置类。
/// </summary>
public static class AppConfig
{
    private static readonly List<Func<UserInfo, Type>> UserMenus = [];

    /// <summary>
    /// 取得应用程序ID。
    /// </summary>
    public static string AppId => "CMSLite";

    /// <summary>
    /// 取得应用程序名称。
    /// </summary>
    public static string AppName => "内容管理系统";

    /// <summary>
    /// 取得或设置页面通用标题。
    /// </summary>
    public static string AppTitle { get; set; }

    /// <summary>
    /// 添加KnownCMS前台应用。
    /// </summary>
    /// <param name="services">依赖注入服务。</param>
    /// <param name="action">配置选项注入。</param>
    public static void AddKnownCMS(this IServiceCollection services, Action<CMSLiteOption> action = null)
    {
        Console.WriteLine(AppName);

        var option = new CMSLiteOption();
        action?.Invoke(option);

        var assembly = typeof(AppConfig).Assembly;
        services.AddKnown(info =>
        {
            //项目ID、名称、类型、程序集
            info.Id = AppId;
            info.Name = AppName;
            info.IsSize = true;
            info.IsLanguage = true;
            info.IsTheme = true;
            info.Assembly = assembly;
            //JS路径，通过JS.InvokeAppVoidAsync调用JS方法
            //info.JsPath = "./script.js";
        });

        //添加服务
        services.AddScoped<ISiteRepository, SiteRepository>();
        services.AddScoped<ISiteService, SiteService>();

        if (option.IsSite)
        {
            //网站端
            DocHelper.Initialize();

            //添加菜单
            AddUserMenu<PostList>();
            AddUserMenu<ReplyList>();

            //添加样式
            KStyleSheet.AddStyle("_content/AntBlazor/css/ant-design-blazor.css");
            KStyleSheet.AddStyle("_content/AntBlazor/css/web.css");
            KStyleSheet.AddStyle("_content/KnownCMS/css/editormd.css");
            KStyleSheet.AddStyle("_content/KnownCMS/css/toc.css");
            KStyleSheet.AddStyle("_content/KnownCMS/css/web.css");

            //添加脚本
            KScript.AddScript("_content/AntBlazor/js/AntBlazor.js");
            KScript.AddScript("_content/KnownCMS/js/editormd.js");
        }
        else
        {
            //管理端
            KStyleSheet.AddStyle("_content/KnownCMS/css/web.css");
        }

        //添加模块
        Config.AddModule(assembly);
    }

    /// <summary>
    /// 添加用户中心菜单。
    /// </summary>
    /// <typeparam name="T">菜单页面类型。</typeparam>
    public static void AddUserMenu<T>()
    {
        UserMenus.Add(u => typeof(T));
    }

    /// <summary>
    /// 添加用户中心菜单。
    /// </summary>
    /// <param name="action">权限菜单委托。</param>
    public static void AddUserMenu(Func<UserInfo, Type> action)
    {
        UserMenus.Add(action);
    }

    internal static List<UserMenuAttribute> GetUserMenus(UserInfo user)
    {
        var menus = new List<UserMenuAttribute>();
        foreach (var item in UserMenus)
        {
            var type = item.Invoke(user);
            if (type == null) continue;

            var menu = type.GetCustomAttribute<UserMenuAttribute>();
            if (menu == null) continue;

            menu.Url = type.GetCustomAttribute<RouteAttribute>()?.Template;
            menus.Add(menu);
        }
        return menus;
    }
}

/// <summary>
/// CMSLite配置选项类。
/// </summary>
public class CMSLiteOption
{
    /// <summary>
    /// 取得或设置是否是前台站点。
    /// </summary>
    public bool IsSite { get; set; } = true;
}